<template>
<div>
  <div class="weui-uploader">
    <p class="weui-uploader__title">藏品照片（{{imgList.length}}/9）</p>
    <div class="weui-uploader__bd">
      <ul class="weui-uploader__files">
        <li class="weui-uploader__file" v-for="(v,k) in imgList" :key="k" :style="'backgroundImage: url('+(resUrl+v.smImageUrl)+')'">
          <!--<span class="remove-img"></span>-->
          <i class="icon icon-cha" @click="removeImg(k)"></i>
          <p class="first-img" v-if="k==0&&v.mediaType == 1">主图</p>
          <p class="phpto-video" v-if="v.mediaType == 2">{{v.duration | timeStamp}}</p>
        </li>
        <li class="uploader__input-box" v-show="!isAndroid&&imgList.length<9">
          <img src="../../../../../../static/images/collection/upload.png" alt="">
          <input  name="files" class="weui-uploader__input" type="file" accept="image/*,video/*" multiple @change="upFile($event)"/>
        </li>
        <li class="uploader__input-box" v-if="isAndroid&&imgList.length<9" @click="showAction">
          <img src="../../../../../../static/images/collection/upload.png" alt="">
        </li>
      </ul>
    </div>
    <p class="uploader-warn">您可以添加一个藏品不同角度的多张照片/视频作为集合，方便日后浏览和分享</p>
  </div>

  <div id="actionSheet_wrap" :class="{actionsheetToggle:isAction}">
    <div class="weui-mask_transparent actionsheet__mask" id="mask"></div>
    <div class="weui-actionsheet" id="weui-actionsheet">
      <div class="weui-actionsheet__menu">
        <div class="weui-actionsheet__cell">
          <p>拍照</p>
          <input class="weui-uploader__input" name="files" type="file" accept="image/*" capture="camera" @change="upFile($event)" @touchend="cancel"/>
        </div>
        <div class="weui-actionsheet__cell">
          <p>摄像</p>
          <input class="weui-uploader__input" name="files" type="file" accept="video/*" capture="camcorder" @change="upFile($event)" @touchend="cancel"/>
        </div>
        <div class="weui-actionsheet__cell">
          <p>从相册选择</p>
          <input class="weui-uploader__input" name="files" type="file" accept="image/*,video/*" @change="upFile($event)" @touchend="cancel"/>
        </div>
      </div>
      <div class="weui-actionsheet__action">
        <div class="weui-actionsheet__cell" @click="cancel">取消</div>
      </div>
    </div>
  </div>

  <div class="uploader-from-box">
    <div class="uploader-from">
      <input class="weui-input" type="text" placeholder="请输入照片名称" v-model="name">
      <div class="dynasty-list">
        <p class="dynasty-select" :class="{selectAll:!dynastyId}" @click="dropDown">{{dynastyName}}</p>
        <ul class="dynasty-dropdown" v-show="isDropDown">
          <li @click="selectDynasty('all')">请选择朝代</li>
          <li v-for="(v,k) in dynastyList" :key="k" @click="selectDynasty(v)">{{v.name}}</li>
        </ul>
      </div>
      <textarea class="weui-textarea" placeholder="藏品其他说明信息，如购买地点、出土地点、预估价等" v-model="presentation"></textarea>
    </div>
    <p class="uploader-warn">以上信息均为非必填项，日后您依然可以在“照片详情”中编辑修改相关信息</p>
  </div>

  <div class="btn-box">
    <button class="weui-btn weui-btn_warn" :class="{btnDefault:!isActive}" type="button" @click="saveCollection">确认</button>
  </div>

</div>
</template>

<script>
  import {ipconfig} from '../../../../../assets/js/ipconfig';
  import {getStatusMapByKey,isAndroid} from '../../../../../assets/js/tools';
  import {saveCollection,uploadMedia,getALLDynastyList} from '../../../../../assets/js/getData';
  export default {
    name: "uploadPhoto",
    data(){
      return {
        resUrl:ipconfig.resUrl,//图片、视频、音频等资源访问路径
        albumImages:null,
        albumMediaList:[],//{mediaurl:null,mediatype:null,duration:null,}
        imgList:[],
        dynastyList:[],//朝代列表
        dynastyId:'all',//当前朝代ID
        dynastyName:'请选择朝代',
        isDropDown:false,//是否显示朝代下拉框
        name:'',
        presentation:'',

        isAction:false,
        isAndroid:null,
      }
    },
    created(){
      this.isAndroid = isAndroid();

      let eStorageUrl = getStatusMapByKey('eStorageUrl');
      this.albumImages = eStorageUrl['albumImages'].val;

      //获取所有朝代
      getALLDynastyList().then(res => {
        if(res.data.reqResult.code > 0){
          this.dynastyList = res.data.data;
        }else {
          Toast(res.data.reqResult.msg);
        }
      })
    },
    computed:{
      isActive(){
        if(this.albumMediaList.length!=0){
          return true;
        }else {
          return false;
        }
      }
    },
    methods:{
      //显示朝代下拉框
      dropDown(){
        this.isDropDown = !this.isDropDown;
      },
      //选择朝代
      selectDynasty(v){
        if(v=='all'){
          this.dynastyId = null;
          this.dynastyName = '请选择朝代';
        }else {
          this.dynastyId = v.id;
          this.dynastyName = v.name;
        }
        this.isDropDown = !this.isDropDown;
      },
      //保存藏品
      saveCollection(){
        if(!this.isActive){
          return;
        }
        let imgNum = 0;
        let videoNum = 0;
        for(let v of this.albumMediaList){
          if(v.mediaType==1){
            imgNum ++;
          }else if(v.mediaType==2){
            videoNum ++;
          }
        }
        let params = {
          name:this.name,
          dynastyId:this.dynastyId,
          presentation:this.presentation,
          imgNum:imgNum,
          videoNum:videoNum,
          albumMediaList:this.albumMediaList,
        };
        saveCollection(params).then(res => {
          if(res.data.reqResult.code > 0){
            Toast('保存藏品成功');
            this.$router.replace({name:'collection.photo'});
          }else {
            Toast(res.data.reqResult.msg);
          }
        })
      },
      //删除图片
      removeImg(k){
        this.imgList.splice(k,1);
        this.albumMediaList.splice(k,1);
      },
      //上传图片
      upFile(event) {
        let files = event.target.files;
        console.log(files);
        let formdata = new FormData();
        formdata.append('type',this.albumImages);
        formdata.append('needSmImg',1);
        for(let i=0;i<files.length;i++){
          formdata.append('files',files[i]);
        }
        uploadMedia(formdata).then(res => {
          if(res.data.reqResult.code > 0){
            let data = res.data.data;
            for(let i=0;i<data.length;i++){
              let obj1 = {};
              obj1.mediaType = data[i].type;
              obj1.mediaUrl = data[i].sourceUrl;
              obj1.smImageUrl = data[i].smImageUrl;
              if(data[i].hasOwnProperty('duration')){
                obj1.duration = data[i].duration;
              }
              this.albumMediaList.push(obj1);
              let obj2 = {};
              obj2.mediaType = data[i].type;
              obj2.smImageUrl = data[i].smImageUrl;
              obj2.mxImageUrl = data[i].mxImageUrl;
              if(data[i].hasOwnProperty('duration')){
                obj2.duration = data[i].duration;
              }
              this.imgList.push(obj2);
            }
            console.log(this.albumMediaList,'2');
          }else {
            Toast(res.data.reqResult.msg);
          }
        })
      },

      showAction(){
        this.isAction = true;
      },
      cancel(){
        this.isAction = false;
      },
    }
  }
</script>

<style scoped>
  .weui-uploader{
    padding: 1.5rem 0.8rem 0.45rem 0.7rem;
    border-bottom: 1px solid #EEEEEE;
  }
  .weui-uploader__title{
    color: #333333;
    font-size: 0.7rem;
    line-height: 0.7rem;
    margin-bottom: 1.25rem;
  }
  .uploader__input-box{
    width: 5.15rem;
    height: 5.15rem;
    position: relative;
    display: inline-block;
    margin-bottom: 0.9rem;
  }
  .weui-uploader__bd{
    margin: 0;
    margin-bottom: 1.2rem;
  }
  .weui-uploader__files{
    display: flex;
    flex-wrap: wrap;
  }
  .weui-uploader__file{
    width: 5.15rem;
    height: 5.15rem;
    margin-right: 0.9rem;
    margin-bottom: 0.9rem;
    background-color: #DEDEDE;
  }
  .weui-uploader__file:nth-child(3n+3){
    margin-right: 0;
  }
  .weui-uploader__file .icon-cha{
    /*width: 1rem;*/
    /*height: 1rem;*/
    /*background: url("../../../../../../static/images/publish/x.png") no-repeat;*/
    /*background-size: contain;*/
    font-size: 1rem;
    color: #666666;
    position: absolute;
    right: -0.4rem;
    top: -0.4rem;
  }
  .first-img{
    background-color: rgba(0,0,0,0.5);
    height: 1rem;
    line-height: 1rem;
    color: #ffffff;
    font-size: 0.65rem;
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
  }
  .phpto-video{
    position: absolute;
    left: 0.7rem;
    bottom: 0.35rem;
    color: #F4F4F4;
    font-size: 0.7rem;
    height: 0.7rem;
    line-height: 0.7rem;
    padding-left: 1.45rem;
    background-image: url("../../../../../../static/images/collection/video.png");
    background-repeat: no-repeat;
    background-size: 0.85rem;
    background-position: left center;
  }
  .uploader-warn{
    color: #333333;
    font-size: 0.55rem;
    line-height: 0.9rem;
    padding-left: 1rem;
    background-image: url("../../../../../../static/images/shiming/warn.png");
    background-repeat: no-repeat;
    background-size: 0.75rem;
    background-position: left 0.1rem;
  }
  /*表单placeholder样式*/
  input::-webkit-input-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input:-moz-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input::-moz-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input:-ms-input-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  textarea::-webkit-input-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea:-moz-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea::-moz-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea:-ms-input-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  .uploader-from-box{
    padding-top: 1rem;
    padding-left: 0.7rem;
    padding-right: 0.7rem;
  }
  .uploader-from{
    width: 12rem;
    margin: 0 auto;
    margin-bottom: 0.7rem;
  }
  .weui-input,.dynasty-list,.weui-textarea{
    width: 100%;
    height: 1.5rem;
    line-height: 1.5rem;
    background-color: #EEEEEE;
    border-radius: 4px;
    padding: 0 0.7rem;
    box-sizing: border-box;
    color: #333333;
    font-size: 0.7rem;
    margin-bottom: 0.9rem;
  }
  .dynasty-list{
    position: relative;
  }
  .dynasty-list:after{
    content: ' ';
    display: inline-block;
    width: 0.45rem;
    height: 0.25rem;
    background-image: url("../../../../../../static/images/collection/sanjiao.png");
    background-repeat: no-repeat;
    background-size: 0.45rem;
    position: absolute;
    top: 0.7rem;
    right: 0.7rem;
    z-index: 10;
  }
  .dynasty-select{
    text-align: left;
    height: 100%;
  }
  .dynasty-select.selectAll{
    color: #666666;
  }
  .dynasty-dropdown{
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0 0 5px 0 rgba(0,0,0,0.3);
    padding: 0.2rem 0;
    max-height: 8rem;
    overflow: auto;
    position: absolute;
    top: 1.65rem;
    left: 0;
    right: 0;
    z-index: 10;
  }
  .dynasty-dropdown li{
    height: 1.3rem;
    line-height: 1.3rem;
    border-bottom: 1px solid #EEEEEE;
    color: #333333;
    font-size: 0.6rem;
    text-align: center;
  }
  .dynasty-dropdown li:last-child{
    border-bottom: none;
  }
  .weui-textarea{
    height: 4.25rem;
    line-height: 0.7rem;
    font-size: 0.55rem;
    padding-top: 0.45rem;
    padding-bottom: 0.45rem;
    margin-bottom: 0;
  }
  .btn-box{
    padding: 0 2rem;
    margin-top: 1.3rem;
    margin-bottom: 2.1rem;
  }
  .weui-btn_warn{
    font-size: 0.7rem;
  }
  .weui-btn.btnDefault{
    background-color: #999999;
  }

  .weui-mask_transparent{
    background: rgba(0, 0, 0, 0.6);
    opacity: 0;
    display: none;
  }
  .weui-actionsheet{
    background-color: #EEEEEE;
  }
  .actionsheetToggle .weui-mask_transparent{
    opacity: 1;
    display: block;
  }
  .actionsheetToggle .weui-actionsheet {
    transform: translate(0, 0);
  }
  .weui-actionsheet__menu{
    background-color: #ffffff;
  }
  .weui-actionsheet__cell{
    padding: 0;
    height: 2.3rem;
    line-height: 2.3rem;
    color: #333333;
    font-size: 0.7rem;
  }
  .weui-actionsheet__cell:before{
    border-top: 1px solid #EEEEEE;
  }
  .weui-actionsheet__action{
    margin-top: 0.25rem;
    background-color: #ffffff;
  }
</style>
